﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using DevExpress.XtraEditors;

using QLTTAN.Form;
using QLTTAN.BLL;
using QLTTAN.DTO;
namespace QLTTAN.UserControls
{
    public partial class ucDanhSachPhongThiXepLop : UserControl
    {
        ThiXepLopBLL TXL_BLL;
        KhoaHocBLL KH_BLL;

        int maxKhoaHoc;

        public ucDanhSachPhongThiXepLop()
        {
            InitializeComponent();
        }

        private void FillDataInLookUpEditKhoaHoc()
        {
            DataTable dt = KH_BLL.KhoaHoc_Load();
            lookUpEditKhoaHoc.Properties.DataSource = dt;
            lookUpEditKhoaHoc.Properties.ValueMember = "MaKhoaHoc";
            lookUpEditKhoaHoc.Properties.DisplayMember = "TenKhoaHoc";

            maxKhoaHoc = dt.Rows.Count - 1;

            lookUpEditKhoaHoc.ItemIndex = maxKhoaHoc;
        }

        private void LoadGridCtrlPhongThiXepLop()
        {
            if (lookUpEditKhoaHoc.EditValue != null)
            {
                try
                {
                    ThiXepLopDTO TXL_DTO = new ThiXepLopDTO();
                    TXL_DTO.MaKhoaHoc = int.Parse(lookUpEditKhoaHoc.EditValue.ToString());

                    gridCtrlPhongThiXepLop.DataSource = TXL_BLL.ThiXepLop_Load(TXL_DTO);

                    if (lookUpEditKhoaHoc.ItemIndex == maxKhoaHoc)
                    {
                        btnThem.Enabled = true;
                        btnXoa.Enabled = true;
                    }
                    else
                    {
                        btnThem.Enabled = false;
                        btnXoa.Enabled = false;
                    }
                }
                catch (Exception ex)
                {
                    XtraMessageBox.Show(ex.Message, "Thông báo!!!", MessageBoxButtons.OK);
                }
            }
        }

        private void ucDanhSachPhongThiXepLop_Load(object sender, EventArgs e)
        {
            TXL_BLL = new ThiXepLopBLL();
            KH_BLL = new KhoaHocBLL();

            FillDataInLookUpEditKhoaHoc();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if(lookUpEditKhoaHoc.EditValue == null)
            {
                XtraMessageBox.Show("Bạn chưa chọn khóa học", "Thông báo!!!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            frmChiTietPhongThiXepLop frmCTPTXL = new frmChiTietPhongThiXepLop();
            frmCTPTXL.MaKhoaHocSelected = int.Parse(lookUpEditKhoaHoc.EditValue.ToString());
            frmCTPTXL.ShowDialog();

            LoadGridCtrlPhongThiXepLop();
        }
        private void btnXem_Click(object sender, EventArgs e)
        {
            if (lookUpEditKhoaHoc.EditValue == null)
            {
                XtraMessageBox.Show("Bạn chưa chọn khóa học", "Thông báo!!!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            frmChiTietPhongThiXepLop frmCTPTXL = new frmChiTietPhongThiXepLop();
            frmCTPTXL.MaTXLSelected = int.Parse(gridViewPhongThiXepLop.GetDataRow(gridViewPhongThiXepLop.FocusedRowHandle)["MaTXL"].ToString());
            frmCTPTXL.ShowDialog();

            LoadGridCtrlPhongThiXepLop();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (gridViewPhongThiXepLop.FocusedRowHandle < 0)
            {
                XtraMessageBox.Show("Bạn chưa chọn phòng thi để xóa.", "Thông báo!!!", MessageBoxButtons.OK);
                return;
            }

            if (XtraMessageBox.Show("Bạn có chắc chắn muốn xóa lớp học này?", "Thông báo!!!", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                try
                {
                    ThiXepLopDTO TXL_DTO = new ThiXepLopDTO();
                    TXL_DTO.MaTXL = int.Parse(gridViewPhongThiXepLop.GetDataRow(gridViewPhongThiXepLop.FocusedRowHandle)["MaTXL"].ToString());

                    if (TXL_BLL.ThiXepLop_Delete(TXL_DTO) > 0)
                        XtraMessageBox.Show("Xóa thành công.", "Thông báo!!!", MessageBoxButtons.OK);
                    else
                        XtraMessageBox.Show("Xóa thất bại.", "Thông báo!!!", MessageBoxButtons.OK);

                    LoadGridCtrlPhongThiXepLop();
                }
                catch (Exception ex)
                {
                    XtraMessageBox.Show(ex.Message, "Thông báo!!!", MessageBoxButtons.OK);
                }
            }
        }

        private void btnKetQuaThiXepLop_Click(object sender, EventArgs e)
        {
            frmKetQuaThiXepLop frmKQTXL = new frmKetQuaThiXepLop();
            frmKQTXL.MaTXLSelected = int.Parse(gridViewPhongThiXepLop.GetDataRow(gridViewPhongThiXepLop.FocusedRowHandle)["MaTXL"].ToString());
            frmKQTXL.ShowDialog();

            LoadGridCtrlPhongThiXepLop();
        }

        private void lookUpEditKhoaHoc_EditValueChanged(object sender, EventArgs e)
        {
            LoadGridCtrlPhongThiXepLop();
        }

        private void gridViewPhongThiXepLop_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "STT")
                e.DisplayText = (e.ListSourceRowIndex + 1).ToString();
        }
    }
}
